package traditionalthread;

public class PPSInterView {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		final ThreadDemo threadDemo= new ThreadDemo();
		new Thread(){
			public void run(){
				while(true){
					int times =threadDemo.ThreadOne();
					if(times==5) break;
				}
			}
		}.start();
		new Thread(new Runnable() {
			
			public void run() {
				while(true){
					int times =threadDemo.ThreadTwo();
					if(times==5) break;
				}
			}
		}).start();
		while(true){
			int times =threadDemo.ThreadMain();
			if(times==5) break;
		}
	}
}
class ThreadDemo{
	int threadMain = 0;
	int threadOne = 0;
	int threadTwo = 0;
	synchronized int ThreadMain(){
		if(threadMain==threadOne&&threadMain==threadTwo){
			threadMain++;
			System.out.println(threadMain+"Times");
			System.out.println(Thread.currentThread().getName());
		}else{
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return threadMain;
	}
	synchronized int ThreadOne(){
		if(threadOne<threadMain){
			System.out.println(Thread.currentThread().getName());
			threadOne++;
		}else{
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return threadOne;
	}
	synchronized int ThreadTwo(){
		if(threadTwo<threadMain){
			System.out.println(Thread.currentThread().getName());
			threadTwo++;
		}else{
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return threadTwo;
	}
}
